Security News
Supply Chain Attack Detected in Solana's web3.js Library
A supply chain attack has been detected in versions 1.95.6 and 1.95.7 of the popular @solana/web3.js library.
The supertest npm package is a high-level abstraction for testing HTTP, allowing you to test your Node.js HTTP servers using a fluent API. It is built on top of the SuperAgent library and provides a simple and flexible way to assert HTTP responses in your tests.
HTTP Assertions
This feature allows you to make assertions on the HTTP response, such as the status code, content type, and body content. The code sample demonstrates how to test an Express.js route.
const request = require('supertest');
const express = require('express');
const app = express();
app.get('/user', function(req, res) {
res.status(200).json({ name: 'john' });
});
describe('GET /user', function() {
it('responds with json', function(done) {
request(app)
.get('/user')
.expect('Content-Type', /json/)
.expect('Content-Length', '15')
.expect(200, done);
});
});
Integration Testing
Supertest can be used for integration testing of an entire application by sending requests to the routes and asserting the expected responses. The code sample shows how to test for both a successful response and a 404 error.
const request = require('supertest');
const app = require('../app');
describe('Integration Testing', function() {
it('responds to /', function(done) {
request(app)
.get('/')
.expect(200, done);
});
it('404 everything else', function(done) {
request(app)
.get('/foo/bar')
.expect(404, done);
});
});
Asynchronous/Await Support
Supertest works with async/await syntax, allowing for cleaner and more readable asynchronous test code. The code sample demonstrates an asynchronous test using async/await.
const request = require('supertest');
const app = require('../app');
describe('Async/Await Support', function() {
it('responds to /', async () => {
await request(app)
.get('/')
.expect(200);
});
});
Axios is a promise-based HTTP client for the browser and Node.js. While it is not specifically designed for testing like supertest, it can be used in combination with testing frameworks to make HTTP requests and assert responses.
Nock is an HTTP server mocking and expectations library for Node.js. Unlike supertest, which performs integration tests against an actual server, nock allows you to test modules that perform HTTP requests by mocking the responses, without having to have an actual server running.
Chai-http is a plugin for the Chai assertion library that enables HTTP assertions. Similar to supertest, it allows for testing HTTP servers, but it is designed to be used with the Chai assertion library, providing a different syntax and additional assertion capabilities.
HTTP assertions made easy via super-agent.
The motivation with this module is to provide a high-level abstraction for testing HTTP, while still allowing you to drop down to the lower-level API provided by super-agent.
You may pass an http.Server
, or a Function
to request()
- if the server is not
already listening for connections then it is bound to an ephemeral port for you so
there is no need to keep track of ports.
SuperTest works with any test framework, here is an example without using any test framework at all:
var request = require('supertest')
, express = require('express');
var app = express();
app.get('/user', function(req, res){
res.send(200, { name: 'tobi' });
});
request(app)
.get('/user')
.expect('Content-Type', /json/)
.expect('Content-Length', '20')
.expect(200)
.end(function(err, res){
if (err) throw err;
});
Here's an example with mocha, note how you can pass done
straight to any of the .expect()
calls:
describe('GET /users', function(){
it('respond with json', function(done){
request(app)
.get('/user')
.set('Accept', 'application/json')
.expect('Content-Type', /json/)
.expect(200, done);
})
})
If you are using the .end()
method .expect()
assertions that fail will
not throw - they will return the assertion as an error to the .end()
callback. In
order to fail the test case, you will need to rethrow or pass err
to done()
, as follows:
describe('GET /users', function(){
it('respond with json', function(done){
request(app)
.get('/user')
.set('Accept', 'application/json')
.expect(200)
.end(function(err, res){
if (err) return done(err);
done()
});
})
})
Anything you can do with superagent, you can do with supertest - for example multipart file uploads!
request(app)
.post('/')
.attach('avatar', 'test/fixtures/homeboy.jpg')
...
Passing the app or url each time is not necessary, if you're testing
the same host you may simply re-assign the request variable with the
initialization app or url, a new Test
is created per request.VERB()
call.
request = request('http://localhost:5555');
request.get('/').expect(200, function(err){
console.log(err);
});
request.get('/').expect('heya', function(err){
console.log(err);
});
Here's an example with mocha that shows how to persist a request and its cookies:
var request = require('supertest')
, should = require('should')
, express = require('express');
var app = express();
app.use(express.cookieParser());
describe('request.agent(app)', function(){
var app = express();
app.use(express.cookieParser());
app.get('/', function(req, res){
res.cookie('cookie', 'hey');
res.send();
});
app.get('/return', function(req, res){
if (req.cookies.cookie) res.send(req.cookies.cookie);
else res.send(':(')
});
var agent = request.agent(app);
it('should save cookies', function(done){
agent
.get('/')
.expect('set-cookie', 'cookie=hey; Path=/', done);
})
it('should send cookies', function(done){
agent
.get('/return')
.expect('hey', done);
})
})
There is another example that is introduced by the file agency.js
You may use any super-agent methods,
including .write()
, .pipe()
etc and perform assertions in the .end()
callback
for lower-level needs.
Assert response status
code.
Assert response status
code and body
.
Assert response body
text with a string, regular expression, or
parsed body object.
Assert header field
value
with a string or regular expression.
Pass a custom assertion function. It'll be given the response object to check. If the response is ok, it should return falsy, most commonly by not returning anything. If the check fails, throw an error or return a truthy value like a string that'll be turned into an error.
Here the string or error throwing options are both demonstrated:
request(app)
.get('/')
.expect(hasPreviousAndNextKeys)
.end(done);
function hasPreviousAndNextKeys(res) {
if (!('next' in res.body)) return "missing next key";
if (!('prev' in res.body)) throw new Error("missing prev key");
}
Perform the request and invoke fn(err, res)
.
Inspired by api-easy minus vows coupling.
MIT
FAQs
SuperAgent driven library for testing HTTP servers
The npm package supertest receives a total of 5,157,301 weekly downloads. As such, supertest popularity was classified as popular.
We found that supertest demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 6 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
A supply chain attack has been detected in versions 1.95.6 and 1.95.7 of the popular @solana/web3.js library.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.